package com.opensource.lzx.restartclient.netty.handler;

import com.alibaba.fastjson.JSON;
import com.opensource.lzx.proto.OperationType;
import com.opensource.lzx.proto.RequestContext;
import com.opensource.lzx.proto.ServiceResponse;
import com.opensource.lzx.restartclient.netty.NettyClientTemplate;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

/**
 * @Author: 739891618@qq.com
 * @Date: 2022-11-13 11:20:01
 * @ClassName: ServiceRegisterHandler
 * @Describe: this is a ...
 */
@Slf4j
public class ServiceRegisterHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RequestContext context = (RequestContext) msg;
        if (context.getResponse().getType().getNumber() == OperationType.REGISTER_RESPONSE.getNumber()) {
            log.info("Service info register success, and receive result : {}",
                    JSON.toJSONString(context.getResponse().getRequestId()));
            CompletableFuture<ServiceResponse> future =
                    NettyClientTemplate.getFutureByRequestId(context.getResponse().getRequestId());
            future.complete(context.getResponse());
        } else {
            super.channelRead(ctx, msg);
        }
    }

}
